package fr.neyb.bernouilli.problem021;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import fr.neyb.bernouilli.common.math.Factorized;
import fr.neyb.bernouilli.common.tools.ClassicImplTools;

public class Entry {

	private static int LIMIT = 10000;

	private static class Couple {
		private final int x;
		private final int y;

		public int sum() {
			return x + y;
		}

		public Couple(int x, int y) {
			this.x = x;
			this.y = y;
		}

		@Override
		public boolean equals(Object o) {
			if (o == null)
				throw new IllegalArgumentException();
			if (!(o instanceof Couple))
				return false;
			Couple c = (Couple) o;
			return c.x == x && c.y == y;
		}

		@Override
		public int hashCode() {
			return ClassicImplTools.hashCode(x, y);
		}
	}

	public static void main(String... args) throws Exception {
		Map<Integer, Integer> dReverseMapping = new HashMap<>();
		Set<Integer> amicals = new HashSet<>();

		for (int curr = 2; curr < LIMIT; ++curr) {
			int d = d(curr);
			Integer find = dReverseMapping.get(curr);
			if (find != null && find.intValue() == d) {
				amicals.add(d);
				amicals.add(curr);
			} else {
				dReverseMapping.put(d, curr);
			}
		}

		int res = 0;
		for (Integer amical : amicals)
			res += amical;
		System.out.println(res);
	}

	public static int d(int x) {
		int res = 0;
		for (Long divisor : new Factorized(x).getDivisors()) {
			if (divisor != x)
				res += divisor;
		}
		return res;
	}

}
